/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docview;

import java.awt.Dimension;
import java.lang.String;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import tableprocessor.CathegoryStaticTree;
import tableprocessor.CathegoryStaticTreeFactory;
import tableprocessor.ColumnPare;
import tableprocessor.Table;
import tableprocessor.PriceDAO;
/**
 *
 * @author Dema
 */
public class ViewNewDoc implements ViewDoc, CathegoryEventListener{

    protected DocTable doc;
    protected DocTable resultDoc;
    //protected BindWindow bindWindow;
    protected Vocabulary vocabulary;
    protected CathegoryView cathegory;
    protected DocTextArea text;
    protected DocTableManager manager ;
    public ViewNewDoc(DocViewManager _m)
    {
       doc = new DocTable(IObserver.Sender.PRICE);
       resultDoc = new DocTable(IObserver.Sender.RESULT);
       //bindWindow = new BindWindow();
       vocabulary = new Vocabulary();
       cathegory = new CathegoryView();
       //******************* commented by Gleb **************
       //text = new DocTextArea(1170,100);
       text = new DocTextArea();
       _m.addNewVocabulary(vocabulary);
       manager = new DocTableManager(doc);
       cathegory.registerEventListener(this);
       cathegory.registerEventListener(manager);
       doc.getColorTable().addMouseListener(manager);

    }


    public JPanel viewDocument(Iterator<Table> it) {

        doc.fill(it);
       // resultDoc.addColumn("1");
        resultDoc.addNewDataRow(new Vector(1));
        doc.setMinimumSize(new Dimension(250,550));
        doc.setPreferredSize(new Dimension(500,550));
        resultDoc.setMinimumSize(new Dimension(250,550));
        resultDoc.setPreferredSize(new Dimension(500,550));
        //bindWindow.setMinimumSize(new Dimension (230,550));
        //bindWindow.setPreferredSize(new Dimension(230,550));
        //bindWindow.setMaximumSize(new Dimension(300,Short.MAX_VALUE));
        cathegory.setMinimumSize(new Dimension(170,550));
        cathegory.setPreferredSize(new Dimension(170,550));
        cathegory.setMaximumSize(new Dimension(170,Short.MAX_VALUE));
        text.setMinimumSize(new Dimension(1170,100));
        text.setPreferredSize(new Dimension(1170,100));
        text.setMaximumSize(new Dimension(1170,100));
        //////////////////////////////////////////////////////////////
        /*bindWindow.addListener((IObserver) doc);
        bindWindow.addListener((IObserver) resultDoc);
        bindWindow.addListener((IObserver) vocabulary);
        vocabulary.addListener((IObserver) doc);
        vocabulary.addListener((IObserver) resultDoc);
        doc.addListener(vocabulary);
        doc.addListener(bindWindow);
        resultDoc.addListener(vocabulary);
        resultDoc.addListener(bindWindow);*/
        cathegory.setListener(resultDoc);
        doc.setListener(text);
        text.setListener(doc);


        //////////////////////////////////////////////////////////////
        JPanel horizontalPanel = new JPanel();
        JPanel verticalPanel = new JPanel();
        horizontalPanel.setLayout(new BoxLayout(horizontalPanel,BoxLayout.LINE_AXIS));
        verticalPanel.setLayout(new BoxLayout(verticalPanel,BoxLayout.Y_AXIS));
        horizontalPanel.add(doc);
        //panel.add(bindWindow);
        horizontalPanel.add(resultDoc);
        horizontalPanel.add(cathegory);
        verticalPanel.add(horizontalPanel);
        verticalPanel.add(text);


        return verticalPanel;

    }

    public void selectCathegoryEvent(DefaultMutableTreeNode node)
    {
        CathegoryStaticTree cst = CathegoryStaticTreeFactory.getInstance();
        if (cst.isHasColumns(node))
        {
           
            Iterator<ColumnPare> iter = cst.getColumnsForCathegory(node);
            if (iter!=null)
            {
                resultDoc.clearTable();
                
               // doc.fillPopupMenu(CathegoryStaticTree.getColumnsForCathegory(node));
                while (iter.hasNext())
                {
                    ColumnPare pare = iter.next();

                    resultDoc.addColumn(pare.getDbcol().getName());
                }
                
            }
        }
    }

};
